package com.imachen.org.controller;

import com.imachen.basic.utils.AjaxResult;
import com.imachen.basic.utils.PageList;
import com.imachen.org.domain.Employee;
import com.imachen.org.query.EmployeeQuery;
import com.imachen.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 员工接口类
 * @author: AChen
 * @date: 2022/8/14 0014 : 19:23
 * @version: ver1.0
 * @email
 */
@Api(value = "员工接口类",description = "员工接口类")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * @description: 查询所有
     * @author: AChen
     * @date: 2022/8/14 0014 : 19:23
     * @param:
     * @return:
     */
    @ApiOperation(value = "查询所有员工信息")
    @GetMapping
    public List<Employee> finAll(){
        return employeeService.findAll();
    }

    /**
     * @description: 查询员工单条数据
     * @author: AChen
     * @date: 2022/8/14 0014 : 19:27
     * @param:
     * @return:
     */
    @ApiOperation(value = "查询员工单条数据")
    @GetMapping("{id}")
    public Employee finOne(@PathVariable("id")Long id){
        return employeeService.findOneById(id);
    }

    /**
     * @description: 删除员工信息
     * @author: AChen
     * @date: 2022/8/14 0014 : 19:29
     * @param:
     * @return:
     */
    @ApiOperation(value = "删除员工信息")
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * @description: 添加或修改员工信息
     * @author: AChen
     * @date: 2022/8/14 0014 : 19:31
     * @param:
     * @return:
     */
    @ApiOperation(value = "添加或修改员工信息")
    @PutMapping
    public AjaxResult add0rUpt(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * @description: 分页查询员工信息
     * @author: AChen
     * @date: 2022/8/14 0014 : 19:34
     * @param:
     * @return:
     */
    @ApiOperation(value = "分页查询员工信息")
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * @description: 批量删除
     * @author: AChen
     * @date: 2022/8/16 0016 : 16:51
     * @param:
     * @return:
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    @GetMapping("/list/{id}")
    public List<Employee> findByShopId(@PathVariable("id")Long shopId){
        return employeeService.findByShopId(shopId);
    }

}
